package com.shop.service.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.service.module.entity.DeptEntity;
import com.shop.service.module.entity.Result;
import com.shop.service.module.service.DeptService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("dept")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/list/user")
    public Result getListUser() {
        return deptService.getListUser();
    }

    @PutMapping("add/user")
    public Result addUser(@RequestParam("ids") Long[] ids, @RequestParam("id") Long id) {
        return deptService.addUser(ids, id);
    }

    @DeleteMapping("/delete/user")
    public Result deleteUser(@RequestParam("id") Long id) {
        return deptService.deleteUser(id);
    }

    @GetMapping("/list/page")
    public Result getListForPage(@RequestParam(value = "pno", defaultValue = "1") Integer pno,
                                 @RequestParam(value = "psize", defaultValue = "10") Integer psize,
                                 @RequestParam(value = "name", defaultValue = "") String name) {
        Page<DeptEntity> page = new Page<>(pno, psize);
        QueryWrapper<DeptEntity> queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        Page<DeptEntity> entityPage = deptService.page(page, queryWrapper);
        List<DeptEntity> list = entityPage.getRecords();
        JSONObject data = new JSONObject();
        data.put("list", list);
        JSONObject jsonPage = new JSONObject();
        jsonPage.put("pno", pno);
        jsonPage.put("psize", psize);
        jsonPage.put("pCount", entityPage.getPages());
        jsonPage.put("totalElements", entityPage.getTotal());
        data.put("page", jsonPage);
        return Result.end(200, data, "查询成功");
    }

    @PutMapping("insert")
    public Result insert(@RequestBody DeptEntity deptEntity) {
        System.out.println(deptEntity);
        if (deptEntity.getName() == null || deptEntity.getName().trim().length() == 0) {
            return Result.end(500, "", "部门名称不可以为空");
        }
        boolean result = deptService.save(deptEntity);
        return result ? Result.end(200, "", "添加成功") : Result.end(500, "", "添加失败");
    }

    @GetMapping("find/id/{id}")
    public Result getDeptById(@PathVariable Integer id) {
        DeptEntity entity = deptService.getById(id);
        return Result.end(200, entity, "");
    }

    @PutMapping("update")
    public Result update(@RequestBody DeptEntity deptEntity) {
        if (deptEntity.getName() == null || deptEntity.getName().trim().length() == 0) {
            return Result.end(500, "", "部门名称不可以为空");
        }
        boolean result = deptService.updateById(deptEntity);
        return result ? Result.end(200, "", "修改成功") : Result.end(500, "", "修改失败");
    }

    @DeleteMapping("delete/id/{id}")
    public Result update(@PathVariable Integer id) {
        boolean result = deptService.removeById(id);
        return result ? Result.end(200, "", "删除成功") : Result.end(500, "", "删除失败");
    }


}
